﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
using System.Configuration;

namespace QuanLyNhaThuoc.HelperClass {
    public static class Common {
        public static string FormatMoneyDisplay(string m, string CCYID) {
            try {
                CultureInfo dk = new CultureInfo("en-US");
                //string m2 = m;
                string m1 = m;

                if (m == "" || m == "0" || m == "0,00" || m == "0.00") {
                    m1 = "0";
                } else {
                    switch (CCYID.Trim()) {
                        case ("VND"):
                        m = m.Replace(",00", "");
                        m1 = Convert.ToDecimal(m, dk).ToString("C", new CultureInfo("vi-VN"));
                        m1 = m1.Replace("₫", "").Replace(",00", "").Trim();
                        break;
                        default:
                        m1 = Convert.ToDecimal(m, dk).ToString("C", new CultureInfo("vi-VN"));
                        m1 = m1.Replace("₫", "").Trim();
                        break;
                    }
                }
                return m1;
            } catch (Exception) {
                return m;
            }
        }
    }

    public static class SYSVAR {
        public static string CCYID = "CCYID";
        public static string STATUS = "STATUS";
        public static string PRODUCTSEARCH = "PRODUCTSEARCH";
        public static string SEARCH = "SEARCH";
    }

    public static class Log {
        /// <summary>
        /// Write log system - TUANTA
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="location"></param>
        /// <param name="MethodName"></param>
        public static void LogError(Exception ex) {
            System.IO.FileStream fs;
            System.IO.StreamWriter sw;
            string strErrorLogFilePath = "";

            try {
                strErrorLogFilePath = ConfigurationManager.AppSettings.Get("LogFilePath");
            } catch {
                strErrorLogFilePath = @"C:\PMOLog.txt";
            }
            if (strErrorLogFilePath == null)
                strErrorLogFilePath = @"C:\PMOLog.txt";

            try {
                if (System.IO.File.Exists(strErrorLogFilePath) == false) {
                    fs = new System.IO.FileStream(strErrorLogFilePath, System.IO.FileMode.CreateNew);
                } else {
                    fs = new System.IO.FileStream(strErrorLogFilePath, System.IO.FileMode.Append);
                }
                sw = new System.IO.StreamWriter(fs);
                string strLine = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.fff: ") +
                    "|Error Description: " + ex.Message;
                if (ex.InnerException != null && ex.InnerException.Message != "")
                    strLine = strLine + "|Inner Exception: " + ex.InnerException.Message;

                sw.WriteLine(Environment.NewLine + strLine + Environment.NewLine + ex.StackTrace);
                sw.Flush();
                sw.Close();
                fs.Close();
            } catch (Exception exn) {
                throw exn;
            }
        }

    }
}